﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HalconDotNet;
using System.Windows;
using Project1.Common;
using Project1.Interface;
namespace Project1.Element
{
    public class Circle : IElem
    {
        private double radius;
        private HObject contourXLD,region;
        private string name;
        private static int existCount = 0;
        private Point centrePoint;
        private int colorPtr = 0;
      
        public void Dispose()
        {
            if (contourXLD != null) contourXLD.Dispose();
            if (region != null) region.Dispose();
            contourXLD = null;
            region = null;
        }

        public Circle()
        {

        }
        public Circle(Point centrePoint,double radius,HObject circleContour,string name )
        {
            this.name = name;
            this.centrePoint = centrePoint;
            this.radius = radius;
            this.contourXLD = new HObject(circleContour);
            HOperatorSet.GenRegionContourXld(contourXLD, out region, "filled");
            HOperatorSet.DilationCircle(region, out region, 10);
        }


        public Circle(Point centrePoint, double radius, HObject circleContour)
        {

            this.centrePoint = centrePoint;
            this.radius = radius;
            this.contourXLD = new HObject(circleContour);
            HOperatorSet.GenRegionContourXld(contourXLD, out region, "filled");
            HOperatorSet.DilationCircle(region, out region, 10);
        }
        public Circle(Circle circle)
        {
            this.name = circle.name;
            this.centrePoint = new Point(circle.centrePoint);
            this.radius = circle.radius;
            this.contourXLD = new HObject(circle.contourXLD);
            this.region = new HObject(circle.region);
        }


        public Point CentrePoint { get { return centrePoint; }  }
        public string GetCentrePointInfo()
        {
            return "中心点:(" + centrePoint.Row + "," + centrePoint.Column + ")";
        }
        public string Name
        {
            get
            {
                return name;
            }
            set
            {
                name = value;
            }
        }

        public double Radius
        {
            get { return radius; }
        }
        public HObject CircleContours
        {
            get
            {
                return contourXLD;
            }
            set
            {
                if (contourXLD != null) { contourXLD.Dispose(); } contourXLD = new HObject(value);
            }
        }

        public HObject Region
        {
            get
            {
                return region;
            }
            set
            {
                if (region != null) { region.Dispose(); } region = new HObject(value);
            }
        }
        public void Reset(int num =0)
        {
            existCount = 0;
        }
        public override string  ToString()
        {
            return "圆:" + name + ",圆心"   + centrePoint+",半径"+radius.ToString("f1");
        }

        public void ApplyChange()
        {
            this.name = "CIRCLE" + existCount++;
        }
        public void dispROI(HTuple windowHandle,string color)
        {
            HOperatorSet.SetColor(windowHandle,color);
            HOperatorSet.SetDraw(windowHandle, "margin");
            HObject roi = null;
            HOperatorSet.GenRegionContourXld(contourXLD, out roi, "margin");
            HOperatorSet.DispObj(roi, windowHandle);
        }

      

    }
}
